/**
 * Created by Administrator on 2018/11/12.
 */
$(function(){

    /* 喜欢 */
    $("#CollLike").on('click',function(){
        var _this = $(this);
        var likeNub = _this.find("#likeNub")
        var likeNmbT = likeNub.text();
        if(_this.hasClass('GiveColl-color')){
			layer.open({
		        content: '是否取消喜欢?',
		        btn: ['确定', '取消'],
		        yes: function (index) {
		        	$.post("/operate/like/" + _this.data('topdiaryid'), {},function(result){
		        		layer.close(index);
		    			if(result.code == 0){
	    			        _this.removeClass('GiveColl-color');
	    			        likeNub.text(Number(likeNmbT) - Number(1))
		    			}else if(result.code == 111){
		    				window.location.href='/singUp';
		    			}else{
		    				sstb.dialog(result.msg);
		    			}
		    		});
		        },
		        no: function () {

		        }
		    });
		}else{
			$.post("/operate/like/" + _this.data('topdiaryid'), {},function(result){
				if(result.code == 0){
			        _this.addClass('GiveColl-color');
			        likeNub.text(Number(likeNmbT) + Number(1));
				}else if(result.code == 111){
					window.location.href='/singUp';
				}else{
					sstb.dialog(result.msg);
				}
			});
		}
        
    })

    /* 收藏 */
    $("#Collection").on('click',function(){
        var _this = $(this);
        if(_this.hasClass('GiveColl-color')){
			 layer.open({
			        content: '是否取消收藏?',
			        btn: ['确定', '取消'],
			        yes: function (index) {
			        	$.post("/operate/collect/" + _this.data('topdiaryid'), {},function(result){
			        		layer.close(index);
							if(result.code == 0){
								 _this.removeClass('GiveColl-color');
							     sstb.dialog("已取消");
							}else if(result.code == 111){
								window.location.href='/singUp';
							}else{
								sstb.dialog(result.msg);
							}
						});
			        },
			        no: function () {

			        }
			    });
		 }else{
			 $.post("/operate/collect/" + _this.data('topdiaryid'), {},function(result){
					if(result.code == 0){
				        _this.addClass('GiveColl-color');
				        sstb.dialog("已收藏");
					}else if(result.code == 111){
						window.location.href='/singUp';
					}else{
						sstb.dialog(result.msg);
					}
				});
		 }
    })
    //加载更多评论
	//初始页数
	var page = 1;
	$("#addload").click(function() {
		var id = $(this).data("id");
		$.get("/load/comment", {page: page,id: id},function(result){
			if(result.code == 0){
				page ++;
				if(result.count > 0){
					 $(".ReviewList").append(result.data);
					 if(!result.hasnext){
						 $('.Headline-Load').hide();
					 }
				}else{
					 $('.Headline-Load').hide();
				}
			}else{
				sstb.dialog(result.msg);
			}
		});
	});

	
	//关注
	$("#follow").click(function() {
		var that = $(this);
		var authorid = that.data('authorid');
		var userid = $("#uid").val();
		var ftype = $('#ftype').data('type');
		console.log(ftype);
		if(userid != ''){
			 if(ftype == 1 || ftype == 2){
				 layer.open({
				        content: '是否取消关注?',
				        btn: ['确定', '取消'],
				        yes: function (index) {
				        	 layer.close(index);
				        	layer.open({
				    	        type: 2
				    	      });
				        	$.ajax({ 
								 url: '/operate/'+ userid + '/cancel/' + authorid, 
								 type: "POST",
						         data: {},
						         dataType: "json",//请求数据类型
								 success: function(data){
									 layer.closeAll();
									 if(data.code == 0){
										 $('#ftype').text("关注")
										 $('#ftype').data("type", 0);
									 }else{
										 sstb.dialog(data.msg);	
									 }
								 },
								 error : function() {
									 sstb.dialog("取消关注异常");	
								 }
							 })	
				        },
				        no: function () {

				        }
				    });
			}else{
				layer.open({
	    	        type: 2
	    	      });
				$.ajax({ 
					 url: '/operate/'+ userid + '/follow/' + authorid, 
					 type: "POST",
			         data: {},
			         dataType: "json",//请求数据类型
					 success: function(data){
						 layer.closeAll();
						 var result = data.data;
						 if(data.code == 0){
							if(result == 1){
								$('#ftype').text("已关注")
								$('#ftype').data("type", 1);
							 }else if(result == 2){
								 $('#ftype').text("互为关注")
								 $('#ftype').data("type", 2);
							 }
						 }else{
							 sstb.dialog(data.msg);
						 }
					 },
					 error : function() {
						 layer.closeAll();
						 sstb.dialog("关注失败");
					 }
				 })
			}
			
		}else{
			window.location.href = "/signup"
		}
	});

})
/* 删除自己评论文章的评论 */
function allDelete(id,that) {
    var _this = $(that);
    layer.open({
        content: ' 确认要删除该评论吗?',
        btn: ['确定', '取消'],
        yes: function (index) {
        	$.post("/operate/delcomment", {id: id},function(result){
				if(result.code == 0){
					window.location.href="/" + result.data.topDiaryUserId + "/article/details/" + result.data.topDiaryId;
				}else{
					if(result.code == 111){
						window.location.href="/signup";
					}else{
						layer.close(index);
						sstb.dialog(result.msg);
					}
				}
			});
        },
        no: function () {

        }
    });
}

function singleDelete(id,that) {
    var _this = $(that);
    layer.open({
        content: ' 确认要删除该评论吗?',
        btn: ['确定', '取消'],
        yes: function (index) {
        	$.post("/operate/delreply", {id: id},function(result){
				if(result.code == 0){
					layer.close(index);
					if(result.count > 0){
						 _this.parents('.ReviewList-col-bg').remove();
						 var num = $('#num').text();
						 $('#num').text(Number(num) - Number(1))
					}else{
					}
				}else{
					if(result.code == 111){
						window.location.href="/signup";
					}else{
						layer.close(index);
						sstb.dialog(result.msg);
					}
				}
			});
        },
        no: function () {

        }
    });
}